{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "dst_dir = \"E:\\\\datasets\\\\spacenet-dataset\\\\spacenet\\\\SN6_buildings\\\\train\\\\AOI_11_Rotterdam\\\\PS-RGB\"\n",
    "selected_file = \"C:\\\\Users\\\\FU\\\\Desktop\\\\urban_list_templet.txt\"\n",
    "filelist_path = \"C:\\\\Users\\\\FU\\\\Desktop\\\\selected.txt\"\n",
    "dst_path = Path(dst_dir)\n",
    "suffix = \".tif\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "class file_reader:\n",
    "    '''\n",
    "    Note:\n",
    "        使用next方法读文件的一行,读完时返回\"End\"\n",
    "    '''\n",
    "    def __init__(self, filename : str):\n",
    "        self.file = open(filename)\n",
    "        self.file.seek(0, 2)\n",
    "        self.size = self.file.tell()\n",
    "        self.file.seek(0, 0)\n",
    "    def next(self) -> tuple:\n",
    "        if self.file.tell() >= self.size:\n",
    "            return \"End\"\n",
    "        curline = self.file.readline()\n",
    "        return curline\n",
    "    def __del__(self):\n",
    "        self.file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "class selected_file_reader(file_reader):\n",
    "    def __init__(self, filename : str, symbol : str = \"None\"):\n",
    "        super().__init__(filename)\n",
    "        self.symbol = symbol\n",
    "    def next(self) -> tuple:\n",
    "        firstline = super().next().strip('\\n')\n",
    "        if firstline == \"End\":\n",
    "            return None\n",
    "        if firstline == self.symbol:\n",
    "            firstline = super().next().strip('\\n')\n",
    "        secondline = super().next().strip('\\n')\n",
    "        if secondline == self.symbol:\n",
    "            secondline = firstline\n",
    "            return firstline, secondline\n",
    "        _ = super().next().strip('\\n')\n",
    "        return firstline, secondline\n",
    "    def __del__(self):\n",
    "        super().__del__()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "fp = open(filelist_path, 'w')\n",
    "\n",
    "sfr = selected_file_reader(selected_file)\n",
    "file_iter = dst_path.iterdir()\n",
    "flag = False\n",
    "while True:\n",
    "    file_list = sfr.next() \n",
    "    if file_list == None:\n",
    "        break\n",
    "    for filepath in file_iter:\n",
    "        filename = str(filepath)\n",
    "        if suffix in filename:\n",
    "            if flag == False and file_list[0] in filename:\n",
    "                flag = True\n",
    "            if flag == True:\n",
    "                # 将文件名写到列表文件中\n",
    "                fp.write(filename+'\\n')\n",
    "            if flag == True and file_list[1] in filename:\n",
    "                flag = False\n",
    "                break\n",
    "\n",
    "fp.close()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "8ab0d968002a578cf1e1aa041721a175249ba338f6da29efa5ef1a380c630376"
  },
  "kernelspec": {
   "display_name": "Python (SOMatch)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
